package com.xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xq.dto.SysDict.SysDictInsertDTO;
import com.xq.dto.SysDict.SysDictListDTO;
import com.xq.dto.SysDict.SysDictUpdateDTO;
import com.xq.entity.SysDictType;
import com.xq.exception.BusinessException;
import com.xq.service.impl.msMapper.MsSysDictTypeMapper;
import com.xq.mapper.SysDictTypeMapper;
import com.xq.service.ISysDictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.vo.DataBaseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author wuven
 * @since 2025-05-28
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private MsSysDictTypeMapper msSysDictTypeMapper;

    @Override
    public void insertDict(SysDictInsertDTO sysDictInsertDTO) {
        SysDictType sysDictType = msSysDictTypeMapper.sysDictDataInsertDtoTOSysDictType(sysDictInsertDTO);
        int insert = sysDictTypeMapper.insert(sysDictType);
        if (insert <=0) {
            throw new BusinessException(DataBaseEnum.INSERTION_FAILED);
        }
    }

    @Override
    public void deleteDict(String dictCode) {
        int i = sysDictTypeMapper.deleteById(dictCode);
        if (i <= 0) {
            throw new BusinessException(DataBaseEnum.DELETE_FAILED);
        }
    }

    @Override
    public void updateDict(SysDictUpdateDTO sysDictUpdateDTO) {
        SysDictType sysDictType = msSysDictTypeMapper.sysDictDataUpdateDtoTOSysDictType(sysDictUpdateDTO);
        int update = sysDictTypeMapper.updateById(sysDictType);
        if (update <= 0) {
            throw new BusinessException(DataBaseEnum.UPDATE_FAILED);
        }
    }

    @Override
    public List<SysDictListDTO> listDict(String userId) {
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectList(queryWrapper);
        List<SysDictListDTO> sysDictListDTOS = new ArrayList<>();
        for (SysDictType sysDictType : sysDictTypes) {
            SysDictListDTO sysDictListDTO = msSysDictTypeMapper.sysDictTypeToSysDictListDTO(sysDictType);
            sysDictListDTOS.add(sysDictListDTO);
        }
        return sysDictListDTOS;
    }
}

